package com.sky.mapper;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ShoppingCartMapper {

    /**
     * 动态条件查询
     * @param shoppingCart
     * @return
     */
    List<ShoppingCart> list(ShoppingCart shoppingCart);

    /**
     * 根据id修改商品数量
     * @param shoppingCart
     */
    @Update("update shopping_cart sc set number = #{number} where id = #{id}  ")
    void updateByNumber(ShoppingCart shoppingCart);

    /**
     * 插入cart信息
     * @param shoppingCart
     */
    @Insert("insert into shopping_cart(name, image, user_id, dish_id, setmeal_id, dish_flavor, number, amount) " +
            "values (#{name},#{image},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount})")
    void insert(ShoppingCart shoppingCart);

    /**
     * 查看购物车
     * @return
     */
    @Select("select * from shopping_cart where user_id = #{userId} ")
    List<ShoppingCart> select(Long userId);

    /**
     * 清空用户购物车
     */
    @Delete("delete from shopping_cart where user_id = #{id}")
    void deleteByUserId(Long id);


    /**
     * 通过id和flavor查询
     * @param shoppingCartDTO
     * @return
     */
    @Select("select * from shopping_cart where dish_id = #{dishId} and dish_flavor = #{dishFlavor} or setmeal_id = #{setmealId} or (dish_id = #{dishId} and dish_flavor is null)")
    ShoppingCart selectById(ShoppingCartDTO shoppingCartDTO);

    /**
     * 通过id和flavor修改number
     * @param cart
     */
    @Update("update shopping_cart set number = #{number} where dish_id = #{dishId} and dish_flavor = #{dishFlavor} or setmeal_id = #{setmealId} or (dish_id = #{dishId} and dish_flavor is null)")
    void changeNumberById(ShoppingCart cart);

    /**
     * 通过id和flavor删除
     * @param cart
     */
    @Delete("delete from shopping_cart where dish_id = #{dishId} and dish_flavor = #{dishFlavor} or setmeal_id = #{setmealId} or (dish_id = #{dishId} and dish_flavor is null)")
    void deleteById(ShoppingCart cart);

    /**
     * 批量插入购物车数据
     *
     * @param shoppingCartList
     */
    void insertBatch(List<ShoppingCart> shoppingCartList);
}
